package com.example.demo01;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * description: FileTest <br>
 * date: 2021/1/26 13:12 <br>
 *
 * @author: ZhangYixin <br>
 * version: 1.0 <br>
 */
public class FileTest {
    public static void main(String[] args) {
        // 文件内容复制
        //fileCopyTest();

        // 全路径打印
        File file = new File("C:/Users/user/Desktop/data");
        List<String> paths = new ArrayList<>();
        getAllFilePaths(file,paths);
        for (String path : paths){
            System.out.println(path);
        }

    }

    /**
     * 文件内容复制
     */
    public static void fileCopyTest(){
        try (
                FileInputStream fis = new FileInputStream("C:/Users/user/Desktop/data/ioTest.txt");
                BufferedInputStream bis = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream("C:/Users/user/Desktop/data/ioTest_copy.txt");
                BufferedOutputStream bos = new BufferedOutputStream(fos);
        ) {
            int size;
            byte[] buf = new byte[64];
            while ( (size = bis.read(buf)) != -1){
                System.out.println(new String(buf));
                fos.write(buf);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void getAllFilePaths(File filePath, List<String> paths){
        File[] files = filePath.listFiles();
        if (files == null){
            return;
        }
        for (File fi : files){
            // 判断是不是文件夹 是文件夹就继续往下
            if (fi.isDirectory()){
                paths.add(fi.getPath());
                getAllFilePaths(fi,paths);
            }else {
                // 不是文件夹的时候就是这个文件的最终路径
                paths.add(fi.getPath());
            }
        }
    }
}
